package com.guagua.chat.statistics;
/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
 * Quartz 定时任务
 * 每天执行
 * @author liyi 
 *
 */
public class StatisticsTaskDay implements Job{

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.add(Calendar.DAY_OF_MONTH, -1);//前一天
		long startTime = calendar.getTimeInMillis();
		calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
		calendar.add(Calendar.DAY_OF_MONTH, -1);//前一天
		
		
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		calendar = Calendar.getInstance();
		
		long endTime = calendar.getTimeInMillis();
		System.out.println(endTime - startTime);
		
		
		/*****************判断是否月最后一天************/
		calendar = Calendar.getInstance();
		boolean isLastDay = IsLastDayByMonth(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
		calendar.add(Calendar.MONTH, 1);//获取下月月份
		int next_month = calendar.get(Calendar.MONTH)+1;
		int next_month_year = calendar.get(Calendar.YEAR);
		
		new StatisticsDao().execStatisticsByDay(startTime,endTime,year,month,day,isLastDay,next_month_year,next_month);
		
		
	
		
	}
	/**
	 * 判断是否本月最后一天
	 * @param year
	 * @param month(0-11)
	 * @param day
	 * @return
	 */
	private static boolean IsLastDayByMonth(int year,int month,int day){
	        Calendar cal = Calendar.getInstance();  
	        cal.set(Calendar.YEAR, year);  
	        cal.set(Calendar.MONTH, month);  
	        // 某年某月的最后一天  
	        return cal.getActualMaximum(Calendar.DATE)==day;  
	}
	
	

}
